[SECTION .gdt]
R3_DATA_SELECTOR equ (6 << 3 | 0b11)
R3_CODE_SELECTOR equ (7 << 3 | 0b11)

[section .text]
[bits 64]

extern get_esp3
extern cmain64

extern kernel_space_base_addr

global user_mode_handler
user_mode_handler:
    call cmain64

    ret

global move_to_user
move_to_user:
    swapgs
    mov rdi, [gs:8]
    swapgs

    call get_esp3

    push R3_DATA_SELECTOR       ; ss
    push rax                    ; rsp
    pushfq                      ; rflags

    xor rax, rax
    mov ax, R3_CODE_SELECTOR
    push rax                    ; cs

    push user_mode_handler      ; rip

    mov ax, R3_DATA_SELECTOR
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax

    iretq